import { sortedUniq } from '@szzj/utils';
import { Form } from 'antd';
import React from 'react';
/**
 * When 逻辑隐藏容器
 * @param props
 * @returns
 */
var When = function When(_ref) {
  var _when$;
  var when = _ref.when,
    children = _ref.children;
  var dependencies = sortedUniq(
    (_when$ = when === null || when === void 0 ? void 0 : when[1]) !== null && _when$ !== void 0
      ? _when$
      : [],
  );
  var formInstance = Form.useFormInstance();
  return when !== null && when !== void 0 && when.length && when.length > 0
    ? /*#__PURE__*/ React.createElement(
        Form.Item,
        {
          noStyle: true,
          dependencies:
            dependencies !== null && dependencies !== void 0 && dependencies.length
              ? dependencies
              : undefined,
          /**
           * @fixme 如果依赖为空，临时设置 shouldUpdate
           * @issue ant-design 表单问题：
           *     如果表单项设置 initialValue，且将该表单项置于 dependencies 中，
           *     antd 不会引起当前表单项重绘，显式设置 shouldUpdate 时则会
           * @todo 需要兼顾无需监听表单数据变更场景，比如表单项在 Form.List 中，部分 props 可通过 name?.[0] 作转换
           */ shouldUpdate:
            dependencies !== null && dependencies !== void 0 && dependencies.length
              ? undefined
              : true,
        },
        function (form) {
          var _when$2;
          var vals = form.getFieldsValue();
          var predicated =
            (_when$2 = when[0]) === null || _when$2 === void 0
              ? void 0
              : _when$2.call(when, vals, form);
          return predicated ? (typeof children === 'function' ? children(form) : children) : null;
        },
      )
    : typeof children === 'function'
    ? children(formInstance)
    : children;
};
export default When;
